import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  Query,
  Put,
  Param,
  Del,
} from '@midwayjs/decorator';
import { ILike } from 'typeorm'
import { StoreManageDTO } from '../dto/storeManage';
import { StoreManageService } from '../service/storeManage';
import { plainToInstance } from 'class-transformer';
import { StoreManageEntity } from '../entity/storeManage';

@Provide()
@Controller('/storeManage') 
export class StoreManageController {
  @Inject()
  storeManageService: StoreManageService;

  @Post('/', { description: '新建' })
  async create(@Body() data: StoreManageDTO) {
    data.freeRoomCount = data.roomCount;
    return await this.storeManageService.create(data.toEntity());
  }

  @Put('/', { description: '编辑' })
  async edit(@Body() data: StoreManageDTO) {
    const storeManage = await this.storeManageService.getById(data.id);
    
    return await this.storeManageService.edit(Object.assign(storeManage, plainToInstance(StoreManageEntity, data)));
  }

  @Del('/:id', { description: '删除' })
  async remove(@Param('id') id: string) {
    const storeManage = await this.storeManageService.getById(id);
    await this.storeManageService.remove(storeManage);
  }

  @Get('/:id', { description: '根据id查询' })
  async getById(@Param('id') id: string) {
    return await this.storeManageService.getById(id);
  }

  @Get('/page', { description: '分页查询' })
  async page(@Query('page') page: number, @Query('size') size: number) {
    return await this.storeManageService.page(page, size);
  }

  @Get('/list', { description: '查询全部' })
  async list(@Query('name') name: string) {
    const where = [{ name: ILike(`%${name}%`)}];
    return await this.storeManageService.list(where);
  }
}